//app.js
var qcloud = require('./vendor/wafer2-client-sdk/index')
var config = require('./config')

App({
    onLaunch: function () {
        qcloud.setLoginUrl(config.service.loginUrl)
    },
    globalData: {},
    toIndex:function(){
      wx.redirectTo({
        url: 'index/index'
      })  
    },
    getUserInfo: function (cb, failcb) {
      var that = this;
      if (this.globalData.userInfo) {
        //已有登录信息
        typeof cb == "function" && cb(this.globalData);
        console.log('已有登录信息');
      } else {
        //无登录信息
        wx.getUserInfo({
          success: function (res) {
            wx.login({
              success: function (res1) {
                var code = res1.code;
                that.globalData.userInfo = res.userInfo;
                console.log('用户信息', that.globalData.userInfo);
                // that.login(code, res, cb);
                wx.redirectTo({
                  url: 'index/index'
                }) 
              }
            })
          }, fail: function (res) {
            typeof failcb == "function" && failcb(res);
            console.log('未授权', res);
          }
        })
      }
    },
    login(callback) {
      wx.login({ // 获取用户资料前，此步骤必需
        success: function (res) {
          if (res.code) {
            var logincode = res.code // 获取匿名识别符，用来鉴别同一用户
            wx.getUserInfo({
              success: function (res) {
                wx.redirectTo({
                  url: 'index/index'
                }) 
              },
              fail: function () {
                wx.showModal({
                  title: '提示',
                  content: '请先授权微信登陆',
                  success: function (res) {
                    if (res.confirm) {
                      console.log('用户点击确定')
                    }
                  }
                })// 当拒绝授权的时候，提示用户需要开启权限，确认时调用微信的权限管理界面
                callback('fail to modify scope', null)
              }
            })
          } else {
            callback('fail to call wx.login()', null)
          }
        }
      });
    }
})
  